<?
//引入所有子系统信息
include_once("../../../common/script/extender_import.php");

//获取板块的设置
include_once("model/setting.php");



//根据页面信息，组装查找语句
//include_once("model/set_page.php");


//根据页面数量信息，筛选信息，创造url，供页码,跳转等使用
include_once("script/set_url.php");




$seo_require="1：标题中的字符尽量控制在65个字符以内；2：首字母大写；3：一定要出现关键词";


//获取数据
$data['data_detail']=select_one_line_all_items($table_prefix."website_data","nid = '".$_REQUEST['nid']."'");


//接口获取网站的信息

    $_url       ="https://evo.enpingshi.com/api/console/seo/site.php?theme_folder=".$data['data_detail']['theme_folder'];

    $_site_seo = file_get_contents($_url);
    $_site_seo = json_decode($_site_seo, true);
    $site_seo=$_site_seo['data'];

    if(!$_site_seo)
    {
        $_url       ="https://evo.crossbow.enpingshi.com/api/console/seo/site.php?theme_folder=".$data['data_detail']['theme_folder'];

        $_site_seo = file_get_contents($_url);
        $_site_seo = json_decode($_site_seo, true);
        $site_seo=$_site_seo['data'];
    }


    $_url       ="https://evo.enpingshi.com/api/console/seo/category.php?theme_folder=".$data['data_detail']['theme_folder'];

    $_category_seo = file_get_contents($_url);
    $_category_seo = json_decode($_category_seo, true);
    $category_seo  =$_category_seo['data'];

    if(!$_category_seo)
    {
        $_url       ="https://evo.crossbow.enpingshi.com/api/console/seo/category.php?theme_folder=".$data['data_detail']['theme_folder'];

        $_category_seo = file_get_contents($_url);
        $_category_seo = json_decode($_category_seo, true);
        $category_seo=$_category_seo['data'];
    }







    if($_REQUEST['ai_seo_site'] == "1")
    {

        //收集数据，整理成content
        $content = "请帮我优化一下我的网站seo;";
        $content .= "结果只返回优化后的结果，不要解释，不要返回其他内容，不要返回其他结果，不要返回其他说明，不要返回其他提示；";
        $content .= "结果不能出现空格和特殊符号，可以出现全格逗号，不要引起js传递参数的时候出错。";
        $content .= "返回结果需要符合这三点要求".$seo_require.";";
        $content .= "我的产分类是".implode(",",transform_2darray_in_1darray_number_as_key($category_seo,"name")).";";
        $content .= "我的公司名字是".$site_seo['name'].";";
        $content .= "返回结果不要任何解析，只要返回一个title";
        $content .= "返回结果必须流畅可读，是句子，能让人读懂，注意用全格逗号断句，优化可读性";


        $url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';

// 若没有配置环境变量，请用百炼API Key将下行替换为：$apiKey = "sk-xxx";
#$apiKey = getenv('DASHSCOPE_API_KEY');
        $apiKey =  "sk-6b9ebd5e8e5d406d8e0e69ec16ea3766";


// 设置请求头
        $headers = [
            'Authorization: Bearer '.$apiKey,
            'Content-Type: application/json'
        ];
// 设置请求体
        $para_data = [
            // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
            //"model" => "qwen-plus",
            //"model" => "qwen-max",
            "model" => "qwen-turbo",
            "messages" => [
                [
                    "role" => "system",
                    "content" => "You are a seo assistant."
                ],
                [
                    "role" => "user",
                    "content" => $content
                ]
            ]
        ];
// 初始化cURL会话
        $ch = curl_init();
// 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($para_data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回不输出结果
// 执行cURL会话
        $response = curl_exec($ch);
// 检查是否有错误发生
        if (curl_errno($ch)) {
            echo 'Curl error: ' . curl_error($ch);
        }
// 关闭cURL资源
        curl_close($ch);
// 输出响应结果

       $_new_site_seo = json_decode($response, true);



       $new_site_seo['title']=$_new_site_seo['choices'][0]['message']['content'];

    }





    if($_REQUEST['ai_seo_site'] == "2")
    {

        //收集数据，整理成content
        $content = "请帮我优化一下我的网站seo;";
        $content .= "结果只返回优化后的结果，不要解释，不要返回其他内容，不要返回其他结果，不要返回其他说明，不要返回其他提示；";
        $content .= "结果不能出现空格和特殊符号，可以出现逗号，不要引起js传递参数的时候出错。";

        $content .= "返回结果需要符合这三点要求 核心关键词必须有，主要关键词和长尾关键词的组合。关键词数量需要符合百度和谷歌对seo要求的数量";
        $content .= "我的产分类是".implode(",",transform_2darray_in_1darray_number_as_key($category_seo,"name")).";";
        $content .= "我的公司名字是".$site_seo['name'].";";
        $content .= "返回结果不要任何解析，只要返回一个keyword";


        $url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';

// 若没有配置环境变量，请用百炼API Key将下行替换为：$apiKey = "sk-xxx";
#$apiKey = getenv('DASHSCOPE_API_KEY');
        $apiKey =  "sk-6b9ebd5e8e5d406d8e0e69ec16ea3766";


// 设置请求头
        $headers = [
            'Authorization: Bearer '.$apiKey,
            'Content-Type: application/json'
        ];
// 设置请求体
        $para_data = [
            // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
            //"model" => "qwen-plus",
            //"model" => "qwen-max",
            "model" => "qwen-turbo",
            "messages" => [
                [
                    "role" => "system",
                    "content" => "You are a seo assistant."
                ],
                [
                    "role" => "user",
                    "content" => $content
                ]
            ]
        ];
// 初始化cURL会话
        $ch = curl_init();
// 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($para_data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回不输出结果
// 执行cURL会话
        $response = curl_exec($ch);
// 检查是否有错误发生
        if (curl_errno($ch)) {
            echo 'Curl error: ' . curl_error($ch);
        }
// 关闭cURL资源
        curl_close($ch);
// 输出响应结果

        $_new_site_seo = json_decode($response, true);



        $new_site_seo['keyword']=$_new_site_seo['choices'][0]['message']['content'];

    }







    if($_REQUEST['ai_seo_site'] == "3")
    {

        //收集数据，整理成content
        $content = "请帮我优化一下我的网站seo;";
        $content .= "结果只返回优化后的结果，不要解释，不要返回其他内容，不要返回其他结果，不要返回其他说明，不要返回其他提示；";
        $content .= "结果不能出现空格和特殊符号，可以出现逗号，不要引起js传递参数的时候出错。";
        $content .= "返回结果需要符合这三点要求".$seo_require.";";
        $content .= "我的产分类是".implode(",",transform_2darray_in_1darray_number_as_key($category_seo,"name")).";";
        $content .= "我的公司名字是".$site_seo['name'].";";
        $content .= "返回结果不要任何解析，只要返回一个description";
        $content .= "返回结果必须流畅可读，是句子，能让人读懂";


        $url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';

// 若没有配置环境变量，请用百炼API Key将下行替换为：$apiKey = "sk-xxx";
#$apiKey = getenv('DASHSCOPE_API_KEY');
        $apiKey =  "sk-6b9ebd5e8e5d406d8e0e69ec16ea3766";


// 设置请求头
        $headers = [
            'Authorization: Bearer '.$apiKey,
            'Content-Type: application/json'
        ];
// 设置请求体
        $para_data = [
            // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
            //"model" => "qwen-plus",
            //"model" => "qwen-max",
            "model" => "qwen-turbo",
            "messages" => [
                [
                    "role" => "system",
                    "content" => "You are a seo assistant."
                ],
                [
                    "role" => "user",
                    "content" => $content
                ]
            ]
        ];
// 初始化cURL会话
        $ch = curl_init();
// 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($para_data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回不输出结果
// 执行cURL会话
        $response = curl_exec($ch);
// 检查是否有错误发生
        if (curl_errno($ch)) {
            echo 'Curl error: ' . curl_error($ch);
        }
// 关闭cURL资源
        curl_close($ch);
// 输出响应结果

        $_new_site_seo = json_decode($response, true);



        $new_site_seo['description']=$_new_site_seo['choices'][0]['message']['content'];

    }
















/*==============================
 * 这里是分类的seo生成
 * ==============================
 */

    if($_REQUEST['ai_seo_category'] == "1")
    {

        //收集数据，整理成content
        $content = "请帮我优化一下我的网站seo;";
        $content .= "结果只返回优化后的结果，不要解释，不要返回其他内容，不要返回其他结果，不要返回其他说明，不要返回其他提示；";
        $content .= "结果不能出现空格和特殊符号，可以出现逗号句号不要引起js传递参数的时候出错。";
        $content .= "返回结果需要符合这三点要求".$seo_require.";";
        $content .= "我的公司名字是".$site_seo['name'].";";
        $content .= "返回结果不要任何解析，只要返回一个title";
        $content .= "返回结果必须包含此分类名称:".$_REQUEST['category']."并且放在标题前面";
        $content .= "把返回结果修饰一下，需要流畅可读，是句子，能让人读懂，注意用全格逗号断句，优化可读性";



        $url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';

// 若没有配置环境变量，请用百炼API Key将下行替换为：$apiKey = "sk-xxx";
#$apiKey = getenv('DASHSCOPE_API_KEY');
        $apiKey =  "sk-6b9ebd5e8e5d406d8e0e69ec16ea3766";


// 设置请求头
        $headers = [
            'Authorization: Bearer '.$apiKey,
            'Content-Type: application/json'
        ];
// 设置请求体
        $para_data = [
            // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
            //"model" => "qwen-plus",
            //"model" => "qwen-max",
            "model" => "qwen-turbo",
            "messages" => [
                [
                    "role" => "system",
                    "content" => "You are a seo assistant."
                ],
                [
                    "role" => "user",
                    "content" => $content
                ]
            ]
        ];
// 初始化cURL会话
        $ch = curl_init();
// 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($para_data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回不输出结果
// 执行cURL会话
        $response = curl_exec($ch);
// 检查是否有错误发生
        if (curl_errno($ch)) {
            echo 'Curl error: ' . curl_error($ch);
        }
// 关闭cURL资源
        curl_close($ch);
// 输出响应结果

        $_new_site_seo = json_decode($response, true);



        $new_category_seo[$_REQUEST['key']]['title']=$_new_site_seo['choices'][0]['message']['content'];
        //$category_seo[$_REQUEST['key']]['title']=$_new_site_seo['choices'][0]['message']['content'];
    }




?>

